package com.demo.hc.biz.area.service;

import com.demo.hc.biz.area.bean.AreaTypeEnum;
import com.demo.hc.biz.area.exception.AreaException;
import com.demo.hc.dal.dao.custom.AreaMapperCustom;
import com.demo.hc.model.mbg.Area;
import com.demo.hc.model.mbg.AreaExample;
import com.demo.hc.model.view.AreaView;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2017-04-17.
 */
@Service
public class AreaService {

    @Autowired
    private AreaMapperCustom areaMapperCustom;

    public Area getAreaById(int id) {
        return areaMapperCustom.selectByPrimaryKey(id);
    }

    public int addArea(int operatorId, Area area) throws AreaException {
        if(operatorId < 0){
            throw new AreaException(AreaException.ILLEGAL_OPERATION);
        }
        Area record = new Area();
        record.setAreaType(area.getAreaType());
        record.setName(area.getName());
        record.setCode(area.getCode());
        record.setPath(area.getPath());
        record.setDistrictId(area.getDistrictId());
        record.setDistrictName(area.getDistrictName());
        record.setCityId(area.getCityId());
        record.setCityName(area.getCityName());
        record.setLongitude(area.getLongitude());
        record.setLatitude(area.getLatitude());
        record.setParentId(area.getParentId());
        record.setAddId(operatorId);
        record.setCreatedTime(new Date());
        record.setOpId(operatorId);
        record.setUpdatedTime(record.getCreatedTime());
        areaMapperCustom.insertSelective(record);
        return record.getId();
    }

    public int updateArea(int operatorId, int areaId, Area area) throws AreaException {
        if(operatorId < 0 || areaId <= 0){
            throw new AreaException(AreaException.ILLEGAL_OPERATION);
        }
        Area record = new Area();
        record.setAreaType(area.getAreaType());
        record.setName(area.getName());
        record.setCode(area.getCode());
        record.setPath(area.getPath());
        record.setDistrictId(area.getDistrictId());
        record.setDistrictName(area.getDistrictName());
        record.setCityId(area.getCityId());
        record.setCityName(area.getCityName());
        record.setLongitude(area.getLongitude());
        record.setLatitude(area.getLatitude());
        record.setParentId(area.getParentId());
        record.setOpId(operatorId);
        record.setUpdatedTime(new Date());
        AreaExample example = new AreaExample();
        AreaExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(areaId);
        return areaMapperCustom.updateByExampleSelective(record,example);
    }

    public int countArea(AreaTypeEnum areaType, Integer parentId){
        Byte type = null;
        if(areaType != null){
            type = NumberUtils.toByte(String.valueOf(areaType.getType()));
        }
        return areaMapperCustom.countArea(type,parentId);
    }

    public List<AreaView> listArea(AreaTypeEnum areaType, Integer parentId){
        Byte type = null;
        if(areaType != null){
            type = NumberUtils.toByte(String.valueOf(areaType.getType()));
        }
        return areaMapperCustom.listArea(type,parentId);
    }


}
